<%--
  Created by IntelliJ IDEA.
  User: hp
  Date: 2018/12/26
  Time: 9:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>仓库查询</title>
    <jsp:include page="${pageContext.request.contextPath}/common/tag.jsp"/>
    <script>
        $(function () {

            /*查询仓库是否有库存小于50的*/
            $.post('${pageContext.request.contextPath}/kucun/queryxiao',function (data) {
                console.log(data);
                if(data.length!=0){
                    $.each(data,function (index,item) {
                        $("#zhuijia").append("<tbody>\n" +
                            "                         <tr>\n" +
                            "                              <td>"+item.sid+"</td>\n" +
                            "                              <td>"+item.sname+"</td>\n" +
                            "                              <td>"+item.ssize+"</td>\n" +
                            "                              <td>"+item.brand.bname+"</td>\n" +
                            "                              <td>"+item.fenlei.fname+"</td>\n" +
                            "                              <td>"+item.supplier.gname+"</td>\n" +
                            "                              <td>"+item.stockNum+"</td>\n" +
                            "                              <td><button class='btn btn-primary' onclick='tianjia("+item.kid+")'>添加</button></td>\n" +
                            "                         </tr>\n" +
                            "                 </tbody>");
                    });

                    $("#buzumodal").modal('show');

                }
            });
            /*绑定下拉数据*/
            $.post("${pageContext.request.contextPath}/fenlei/queryfenlei",function (data) {
                $.each(data,function (index,item) {
                    $("#typeid").append("<option value=\""+item.fid+"\">"+item.fname+"</option>")
                    //$("#fenlei").append("<option value=\""+item.fid+"\">"+item.fname+"</option>")
                })
            });
            /*绑定下拉数据*/
            $.post("${pageContext.request.contextPath}/brand/query",function (data) {
                $.each(data,function (index,item) {
                    $("#brandid").append("<option value=\""+item.bid+"\">"+item.bname+"</option>")
                    //$("#dianpu").append("<option value=\""+item.shopId+"\">"+item.shopName+"</option>")
                })
            });
            /*绑定下拉数据*/
            $.post("${pageContext.request.contextPath}/company/querydanwei",function (data) {
                $.each(data,function (index,item) {
                    $("#danwei").append("<option value=\""+item.cid+"\">"+item.cname+"</option>")
                })
            });
            /*信息表单*/
            $("#mytable").bootstrapTable({
                url : '${pageContext.request.contextPath}/kucun/query',
                toolbar : '#addkucun',
                striped :true,
                pageSize : 10,
                pageList : [2,4,8,16],
                pageNumber : 1,
                height : 700,
                pagination : true,
                sidePagination:'server',  //启用服务器端分页
                queryParams:function(params){
                    var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
                        limit: params.limit,  //页面大小
                        offset: params.offset, //页码
                        name:$("#shopname").val(),
                        typeid:$("#typeid").val(),
                        sbrand:$("#brandid").val()
                    };
                    return temp;
                },
                columns : [
                    //{field : 'kid',title : '编号',align : 'center'},
                    {field : 'sid',title : '商品编号',align : 'center'},
                    {field : 'sname',title : '商品名称',align : 'center'},
                    {field : 'ssize',title : '尺寸',align : 'center'},
                    {field : 'scolor',title : '颜色',align : 'center'},
                    {field : 'fenlei.fname',title : '分类',align : 'center'},
                    {field : 'brand.bname',title : '品牌',align : 'center'},
                    {field : 'goldMoney',title : '进价',align : 'center'},
                    {field : 'saleMoney',title : '售价',align : 'center'},
                    {field : 'stockNum',title : '库存数量',align : 'center'},
                    {field : 'costMoney',title : '成本金额',align : 'center'},
                    /*{title:'操作',align:'center',
                        formatter : function (index,row,value) {
                            return "<button class='btn btn-primary' onclick=\"updateMoney("+row.kid+",'"+row.sname+"',"+row.goldMoney+","+row.stockNum+")\">修改进价</button>&nbsp;&nbsp;&nbsp;" +
                                "<!--<button class='btn btn-primary'>修改</button>-->"
                        }
                    }*/
                ]
            })
            /*添加校验*/
            $("#addform").bootstrapValidator({
                message: 'This value is not valid',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    sname : {
                        message: '请输入商品名称',
                        validators: {
                            notEmpty: {
                                message: '商品名称不能为空！'
                            }
                        }
                    },
                    guige : {
                        message: '请输入商品规格',
                        validators: {
                            notEmpty: {
                                message: '商品规格不能为空！'
                            }
                        }
                    },
                    fenlei : {
                        message: '请选择分类',
                        validators: {
                            notEmpty: {
                                message: '请选择商品分类！'
                            },
                            callback: {
                                message: '请选择商品分类',
                                callback: function(value, validator) {
                                    if (value == 0) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    },
                    jinjia : {
                        message: '请输入商品进价',
                        validators: {
                            notEmpty: {
                                message: '商品进价金额不能为空！'
                            },
                            numeric: {
                                message: '进价金额只能输入数字'
                            }
                        }
                    },
                    shoujia : {
                        message: '请输入商品售价',
                        validators: {
                            notEmpty: {
                                message: '商品售价金额不能为空！'
                            },
                            numeric: {
                                message: '售价金额只能输入数字'
                            }
                        }
                    },
                    kucunNum : {
                        message: '请输入商品库存',
                        validators: {
                            notEmpty: {
                                message: '商品库存数量不能为空！'
                            },
                            numeric: {
                                message: '商品库存数量只能输入数字'
                            }
                        }
                    },
                    dianpu : {
                        message: '请选择店铺',
                        validators: {
                            notEmpty: {
                                message: '请选择店铺！'
                            },
                            callback: {
                                message: '请选择店铺',
                                callback: function(value, validator) {
                                    if (value == 0) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    },
                    danwei : {
                        message: '请选择单位',
                        validators: {
                            notEmpty: {
                                message: '请选择单位！'
                            },
                            callback: {
                                message: '请选择单位',
                                callback: function(value, validator) {
                                    if (value == 0) {
                                        return false;
                                    } else {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            })
        })
    </script>
</head>
<body style="margin:20px 10px;overflow-y: hidden;">
    <div class="">
        <form class="form-inline" style="margin-top: 20px;text-align: right">
            <div class="form-group">
                <label style="font-size: 15px;">商品分类：</label>&nbsp;
                <select name="type" id="typeid" class="form-control">
                    <option value="0">全部</option>
                </select>
            </div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <div class="form-group">
                <label style="font-size: 15px;">品牌：</label>&nbsp;
                <select name="shop" id="brandid" class="form-control">
                    <option value="0">全部</option>
                </select>
            </div> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <div class="form-group">
                <label for="shopname">商品名称：</label>&nbsp;
                <input type="text" class="form-control" id="shopname" placeholder="三只松鼠" style="height: 40px;">
            </div>
            <button onclick="serch();" class="btn btn-primary btn-lg" type="button"><span class="glyphicon glyphicon-search">搜索</span></button>
        </form>
        <%--<button class="btn btn-primary" id="addkucun" onclick="addmodal()">添加</button>--%>
        <table id="mytable"></table>
    </div>
    <%--修改进价模态框--%>
    <div class="modal fade" id="xiujia">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">修改进价</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">商品名称：</label>
                            <div class="col-sm-9">
                                <p class="form-control-static" id="mingcheng"></p>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="xiujinjian" class="col-sm-3 control-label">修改价钱：</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="xiujinjian">
                            </div>
                        </div>
                        <input type="hidden" class="form-control" id="hidkid">
                        <input type="hidden" class="form-control" id="kucunnum">
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="bcjinjia()">保存</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
    <%--仓库不足提示框--%>
    <div class="modal fade bs-example-modal-lg" id="buzumodal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content" style="padding: 20px 50px;width: 1200px;margin-left: -160px;">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h1 class="modal-title" style="color: red;text-align: center">库存紧急</h1>
                    <h5 style="text-align: center;">以下是库存为0或小于20的商品</h5>
                </div>
                <div class="modal-body">
                    <div>
                        <table class="table table-hover" id="zhuijia">
                            <thead>
                            <tr>
                                <th>商品编号</th>
                                <th>商品名称</th>
                                <th>尺码</th>
                                <th>品牌</th>
                                <th>分类</th>
                                <th>供货商</th>
                                <th>当前库存</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                        </table>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <%--<div class="modal fade" id="buzumodal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h1 class="modal-title" style="color: red;text-align: center">库存紧急</h1>
                    <h5 style="text-align: center;">以下是库存为0或小于20的商品</h5>
                </div>
                <div class="modal-body">
                    <div>
                        <table class="table table-hover" id="zhuijia">
                            <thead>
                                <tr>
                                    <th>商品编号</th>
                                    <th>商品名称</th>
                                    <th>尺码</th>
                                    <th>品牌</th>
                                    <th>分类</th>
                                    <th>供货商</th>
                                    <th>当前库存</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    &lt;%&ndash;<button type="button" class="btn btn-primary"></button>&ndash;%&gt;
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>--%>
    <%--添加库存的模态框--%>
    <div class="modal inmodal" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content animated bounceInLeft">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                    </button>
                    <i class="fa fa-laptop modal-icon"></i>
                    <h4 class="modal-title">添加</h4>
                    <%--<small class="font-bold"></small>--%>
                </div>
                <div class="modal-body">
                    <form id="addform" class="form-horizontal"  method="post" role="form">
                        <%--商品id--%>
                        <input type="hidden" value="0" name="id" id="id">
                        <%--得到当前页码--%>
                        <%--<input type="hidden" name="pn">--%>
                            <div class="form-group">
                                <label for="sname" class="col-sm-3 control-label">商品名称：</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" name="sname" id="sname" placeholder="请输入商品名称">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="guige" class="col-sm-3 control-label">规格：</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" name="guige" id="guige" placeholder="请输入商品规格">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="fenlei" class="col-sm-3 control-label">商品分类：</label>
                                <div class="col-sm-9">
                                    <select name="fenlei" id="fenlei" class="form-control">
                                        <option value="0">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="jinjia" class="col-sm-3 control-label">进价：</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" name="jinjia" id="jinjia" placeholder="请输入商品进价">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="shoujia" class="col-sm-3 control-label">售价：</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" name="shoujia" id="shoujia" placeholder="请输入商品售价">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="kucun" class="col-sm-3 control-label">库存：</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" name="kucunNum" id="kucun" placeholder="请输入库存">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="dianpu" class="col-sm-3 control-label">店铺：</label>
                                <div class="col-sm-9">
                                    <select name="dianpu" id="dianpu" class="form-control">
                                        <option value="0">请选择</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="danwei" class="col-sm-3 control-label">单位：</label>
                                <div class="col-sm-9">
                                    <select name="danwei" id="danwei" class="form-control">
                                        <option value="0">请选择</option>
                                    </select>
                                </div>
                            </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="addKucun()">保存</button>
                </div>
            </div>
        </div>
    </div>
</body>

<script>
    /*修改金价保存*/
    function bcjinjia() {
        var hidkid = $("#hidkid").val();
        var jinjia = $("#xiujinjian").val();
        var kucun = $("#kucunnum").val();
        $.post("${pageContext.request.contextPath}/kucun/updatejinjia",{"hidkid":hidkid,"jinjia":jinjia,"kucun":kucun},function (data) {
            $("#mytable").bootstrapTable('load',data);
            $("#xiujia").modal('hide');
        })
    }
    /*获得修改进价商品信息*/
    function updateMoney(id,name,jinjia,kucun) {
        /*显示模态框*/
        $("#xiujia").modal('show');
        $("#hidkid").val(id);
        $("#mingcheng").html(name);
        $("#xiujinjian").val(jinjia);
        $("#kucunnum").val(kucun);
    }
    /*点击搜索*/
    function serch() {
        //返回表格的 Options。
        var opts = $("#mytable").bootstrapTable("getOptions") ;
        var limit = opts.pageSize ;
        var offset = (opts.pageNumber-1) * limit;
        var name = $("#shopname").val() ;
        var typeid = $("#typeid").val();
        var brandid = $("#brandid").val();
        $.post("${pageContext.request.contextPath}/kucun/query",{'name':name,'typeid':typeid,'sbrand':brandid,'limit':limit,'offset':offset},function (data) {
            // var mydata = eval("("+data+")") ;
            //重新绑定表格数据
            $("#mytable").bootstrapTable('load',data) ;
        })
    }

    function addmodal() {
        $("#myModal").modal('show');
        document.getElementById("addform").reset();
        $("#addform").data('bootstrapValidator').resetForm();//清除当前验证的关键之处
    }

    function addKucun() {
        //启用较验
        $("#addform").data("bootstrapValidator").validate();
        var flag = $("#addform").data("bootstrapValidator").isValid() ;
        if(flag){
            $("#sname").val();
            $("#guige").val();
            $("#fenlei").val();
            $("#jinjia").val();
            $("#sname").val();
            $("#sname").val();
            $("#sname").val();
            $("#sname").val();
        }
    }

    function tianjia(kid) {
        $.post('${pageContext.request.contextPath}/kucun/lsadd',{'kid':kid},function (data) {
            if(data.count==1){
                window.location.href = '/caigou/JinHuo.jsp';
            }
        })
    }
</script>
</html>
